package de.nroz.control;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import de.nroz.model.Artikel;
import de.nroz.model.ArtikelChangedListener;
import de.nroz.model.ArtikelModel;
import de.nroz.model.Images;
import de.nroz.view.ViewArtikel;
import de.nroz.view.ViewGewerbe;

public class ControlGewerbe {
	private ArtikelModel model = null;

	private ViewGewerbe view = null;

	private final ArtikelListener changeArtikelListener = new ArtikelListener();

	public ControlGewerbe(ArtikelModel m, ViewGewerbe v) {
		this.model = m;
		this.view = v;
		initListeners();
		calcTotalAmounts();
	}

	private void initListeners() {
		view.getTableView().requestFocus();
		view.getTableView().addKeyListener(new TableActionListener());
		view.getFrame().addWindowListener(new CloseWindowListener());
		view.addDeleteAction(new DeleteAction("Löschen", Images.remove));
		view.addInsertAction(new AddAction("Hinzufügen", Images.add));
		view.addEditAction(new EditAction("Ändern", Images.edit));
		view.addCloneAction(new CloneAction("Duplizieren", Images.edit));
		view.getTableView().addMouseListener(new TableMouseListener());

	}

	public void addArtikel() {
		Artikel art = new Artikel();
		int column = view.getTableView().getSelectedColumn();
		model.add(art);
		view.getTableView().changeSelection(model.getRowCount() - 1, column,
				false, false);
		DBUtil.getInstance().insertOrUpdate(art);
		changeArtikelListener.changedArtikel();
	}

	public void cloneArtikel() {
		int row = view.getTableView().getSelectedRow();
		int column = view.getTableView().getSelectedColumn();
		if (row != -1) {
			Artikel art = model.get(
					view.getTableView().convertRowIndexToModel(row)).clone();
			model.add(art);
			view.getTableView().changeSelection(row, column, false, false);
			DBUtil.getInstance().insertOrUpdate(art);
			changeArtikelListener.changedArtikel();
		}
	}

	public void deleteArtikel() {
		int option = JOptionPane.showConfirmDialog(view.getFrame(),
				"Ausgewählten Artikel wirklich löschen?", "Löschen",
				JOptionPane.YES_NO_OPTION);
		if (JOptionPane.OK_OPTION == option) {
			int row = view.getTableView().getSelectedRow();
			int column = view.getTableView().getSelectedColumn();
			if (row != -1) {
				Artikel art = model.get(view.getTableView()
						.convertRowIndexToModel(row));
				view.getTableView().changeSelection(row - 1, column, false,
						false);
				model.delete(art);
				DBUtil.getInstance().delete(art);
				changeArtikelListener.changedArtikel();
			}
		}
	}

	public void editArtikel() {
		int row = view.getTableView().getSelectedRow();
		if (row != -1) {
			Artikel art = model.get(view.getTableView().convertRowIndexToModel(
					row));
			new ViewArtikel(view.getFrame(), art, changeArtikelListener);
		}
	}

	public void calcTotalAmounts() {
		BigDecimal totalEkPreis = new BigDecimal(0.00d);
		BigDecimal totalVkPreis = new BigDecimal(0.00d);
		long totalEk = 0;
		long totalVk = 0;
		for (int i = 0; i < model.getRowCount(); i++) {
			Artikel artikel = model.get(i);
			if (artikel.getEkRPreis() != null) {
				totalEkPreis = totalEkPreis.add(artikel.getEkRPreis());
				totalEk += 1;

			}

			if (artikel.getVkPreis() != null) {
				totalVkPreis = totalVkPreis.add(artikel.getVkPreis());
				totalVk += 1;
			}
		}
		view.setTotals(totalEk, totalVk, totalEkPreis, totalVkPreis);
	}

	class ArtikelListener implements ArtikelChangedListener {

		@Override
		public void changedArtikel() {
			model.fireTableDataChanged();
			calcTotalAmounts();
		}

	}

	class TableActionListener implements KeyListener {
		@Override
		public void keyPressed(KeyEvent e) {

		}

		@Override
		public void keyReleased(KeyEvent e) {

			switch (e.getKeyCode()) {
			case KeyEvent.VK_CONTROL:
				editArtikel();
				break;
			case KeyEvent.VK_DELETE:
				deleteArtikel();
				break;
			case KeyEvent.VK_INSERT:
				addArtikel();
				break;
			}
		}

		@Override
		public void keyTyped(KeyEvent e) {
		}
	}

	class TableMouseListener implements MouseListener {

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			if (e.isPopupTrigger()) {
				view.getTableView().changeSelection(
						view.getTableView().rowAtPoint(e.getPoint()), 0, false,
						false);
				view.showPopup(view.getTableView(), e.getX(), e.getY());
			} else if (e.getClickCount() > 1) {
				editArtikel();
			}
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			if (e.isPopupTrigger()) {
				view.getTableView().changeSelection(
						view.getTableView().rowAtPoint(e.getPoint()), 0, false,
						false);
				view.showPopup(view.getTableView(), e.getX(), e.getY());
			}
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub

		}

		@Override
		public void mouseClicked(MouseEvent e) {
		}
	}

	class EditAction extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5585671130509184518L;

		public EditAction(String text, ImageIcon icon) {
			super(text, icon);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			editArtikel();
		}
	}

	class AddAction extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5585671130509184518L;

		public AddAction(String text, ImageIcon icon) {
			super(text, icon);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			addArtikel();
		}
	}

	class DeleteAction extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5585671130509184518L;

		public DeleteAction(String text, ImageIcon icon) {
			super(text, icon);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			deleteArtikel();
		}
	}

	class CloneAction extends AbstractAction {

		/**
		 * 
		 */
		private static final long serialVersionUID = 5585671130509184518L;

		public CloneAction(String text, ImageIcon icon) {
			super(text, icon);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			cloneArtikel();
		}
	}
}
